#include <stdio.h>
#include <unistd.h>
#include <getopt.h>

int main(int argc, char **argv)
{
	int c;
	struct option opts[] = {
		{"help", no_argument, NULL, 'p'},
		{"test", no_argument, NULL, 't'},
		{0, 0, 0, 0}
	}; 

	/*
	 	可执行文件识别选项
			-a 输出hello
			-h 输出world
			-o 下课了
			-d 打游戏的滚蛋
	 */
	while (1) {
// 		c = getopt(argc, argv, "-aho::d:");
		c = getopt_long(argc, argv, "-aho::d:", opts, NULL);
		if (-1 == c)
			break;
		switch (c) {
			case 1:
				printf("非选项参数\n");
				break;
			case 'a':
				printf("hello\n");
				break;
			case 'h':
				printf("world\n");
				break;
			case 'o':
				if (optarg) {
					printf("可选参数:%s\n", optarg);
				}
				printf("下课了\n");
				break;
			case 'd':
				printf("参数:%s\n", optarg);
				printf("滚蛋了\n");
				break;
			case '?':
				printf("不认识\n");
				break;
			case 'p':
				printf("help文档\n");
				break;
			case 't':
				printf("测试的长格式\n");
				break;
			default:
				break;
		}
	}

	return 0;
}

